﻿
function zeroBlanks(formname) {
	var i, ctrl;
	for (i = 0; i < formname.elements.length; i++) {
		ctrl = formname.elements[i];
		if (ctrl.type == "text") {
			if (makeNumeric(ctrl.value) == "")
				ctrl.value = "0";
		}
	}
}

function filterChars(s, charList) {
	var s1 = "" + s; // force s1 to be a string data type
	var i;
	for (i = 0; i < s1.length;) {
		if (charList.indexOf(s1.charAt(i)) < 0)
			s1 = s1.substring(0, i) + s1.substring(i + 1, s1.length);
		else
			i++;
	}
	return s1;
}

function makeNumeric(s) {
	return filterChars(s, "1234567890.-");
}

function numval(val, digits, minval, maxval) {
	val = makeNumeric(val);
	if (val == "" || isNaN(val)) val = 0;
	val = parseFloat(val);
	if (digits != null) {
		var dec = Math.pow(10, digits);
		val = (Math.round(val * dec)) / dec;
	}
	if (minval != null && val < minval) val = minval;
	if (maxval != null && val > maxval) val = maxval;
	return parseFloat(val);
}

function formatNumber(val, digits, minval, maxval) {
	var sval = "" + numval(val, digits, minval, maxval);
	var i;
	var iDecpt = sval.indexOf(".");
	if (iDecpt < 0) iDecpt = sval.length;
	if (digits != null && digits > 0) {
		if (iDecpt == sval.length)
			sval = sval + ".";
		var places = sval.length - sval.indexOf(".") - 1;
		for (i = 0; i < digits - places; i++)
			sval = sval + "0";
	}
	var firstNumchar = 0;
	if (sval.charAt(0) == "-") firstNumchar = 1;
	for (i = iDecpt - 3; i > firstNumchar; i -= 3)
		sval = sval.substring(0, i) + "," + sval.substring(i);

	return sval;
}

function presentValue(fv, r, y) {
	return fv / Math.pow(1 + r, y);
}

function futureValue(p, r, y) {
	return p * Math.pow(1 + r, y);
}

function returnRate(pv, fv, y) {
	return Math.pow(fv / pv, 1.0 / y) - 1.0;
}

function geomSeries(z, m, n) {
	var amt;
	if (z == 1.0) amt = n + 1;
	else amt = (Math.pow(z, n + 1) - 1) / (z - 1);
	if (m >= 1) amt -= geomSeries(z, 0, m - 1);
	return amt;
}

function basicInvestment(p, r, y, c) {
	if (c == null) c = 0;

	return futureValue(p, r, y) + c * geomSeries(1 + r, 1, y);
}

function annuityPayout(p, r, y) {
	return futureValue(p, r, y - 1) / geomSeries(1 + r, 0, y - 1);
}

function mortgagePayment(p, r, y) {
	return futureValue(p, r, y) / geomSeries(1 + r, 0, y - 1);
}

function randN(m, s) {
	return s * Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random()) + m;
}

function logNmean(m, s) {
	return Math.log(m) - (Math.pow(logNsigma(m, s), 2) / 2);
}

function logNsigma(m, s) {
	return Math.sqrt(Math.log(Math.pow(s / m, 2) + 1));
}

function gmEst(r_am, s) {
	return Math.sqrt(Math.pow(1 + r_am, 2) - Math.pow(s, 2)) - 1;
}

function numOrder(n, m) {
	return n - m;
}

function getParameterByName(url, name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(url);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function setPagingSort(id, newSort) {
    $("#" + id + " .pagination a").each(function (index) {
        var href = $(this).attr('href');
        if (href) {
            var currentSort = getParameterByName(href, 'sort');

            if (currentSort) {
                var decoded = decodeURIComponent(href);
                $(this).prop('href', decoded.replace("sort=" + currentSort, "sort=" + newSort))
            }
            else {
                $(this).prop('href', href + "&sort=" + newSort);
            }
        }
    });
}

function setInitSort(id, newSort)
{
    var sortArr = newSort.split(',')
    var sorting = [[sortArr[0], sortArr[1]]];
    $("#" + id + " table").trigger("sorton", [sorting]);
}
function RemoveTime(id) {
    $(id).val($(id).val().replace(' 00:00:00', ''))
}
//-->
